// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.cmds;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

import javax.swing.JLabel;
import javax.swing.JPanel;

import tc.common.registry.RegistrySys;
import tc.common.symbology.Style;
import tc.common.utility.Dynbuff;
import tc.common.utility.DynbuffLine;
import tc.common.utility.DynbuffText;
import tc.common.utility.GrView;
import tc.common.utility.TcDisplayTransf;
import tc.common.utility.TcDisplayXY;
import tc.crase.db.XattrGIT;
import tc.crase.db.XattrGRP;
import tc.crase.db.XattrGXY;
import tc.crase.db.Xdb;
import tc.crase.db.Xobject;
import tc.crase.sys.XdbEx;
import tc.crase.sys.XobjectEx;
import tc.crase.view.GrDocGraph;
import tc.crase.view.ViewGraph;

public class CmdAdd  extends CmdEx  implements ActionListener {
	
	Point2D.Double xy;
	JLabel selectedLabel;
	boolean clicked = false;
	
	public CmdAdd () {
	}
	
	public void initializeDialog() {
		super.initializeDialog();
		setLabel("Add in Graph.");
		setHint("Select object and enter one in graph.");
		
		Selection.objectsAllowed = true;
		Selection.attrsAllowed   = false;
		
		selectedLabel  = new JLabel("");
		
		dialog.add(selectedLabel);
		
		finalizeDialog();
		
		Selection.objectsAllowed = true;
	}
	
	public void enterPoint(Point pt, GrView view) {
		if (Selection.selset.size() == 0) return;
		if (view instanceof ViewGraph == false) return;
		
		ViewGraph viewGraph = (ViewGraph)view;
		GrDocGraph grDocGraph = viewGraph.getGrDocGraph();
		if (grDocGraph == null) return;
		
		XobjectEx xobj = (XobjectEx)Selection.selset.get(0);
		
		XattrGRP attrGraph = grDocGraph.getDocGraph();
		
		for ( XattrGIT item : attrGraph) {
			if (item instanceof XattrGXY == false) continue;
			if ( item.getParent() == xobj ) return;
		}
		
		TcDisplayXY PT = new TcDisplayXY(new Point2D.Double(pt.x, pt.y),
				view.transf, TcDisplayXY.CoordSys.DEVICE);
		PT.transform(TcDisplayXY.CoordSys.WORLD);
				
		RegistrySys.registry.lock(); {
			attrGraph.createGXY(xobj, PT.xy);
			Selection.clean();
		} RegistrySys.registry.unlock();
		
		Dynbuff.dynamicBufferClean();
		
		clicked = true;
	}
	
	public void enterSelected(Point pt, GrView view, Object object) {
		if (clicked) {
			clicked = false;
			return;
		}
		
		RegistrySys.registry.lock(); {
			Selection.clean();
			
			if (object instanceof XobjectEx ) {
				Selection.add((XobjectEx)object);
			}

		} RegistrySys.registry.unlock();
	}

	
	public void enterReset(Point pt, GrView view) {
		RegistrySys.registry.lock(); {
			Selection.clean();
		} RegistrySys.registry.unlock();
		
		Dynbuff.dynamicBufferClean();
	}
	
	public Style getStyle(XobjectEx object) { //TODO - common method
		Style style = null;
		
		Xdb xdb = object.getDb();
		if (xdb instanceof XdbEx) {
			XdbEx xdbex = (XdbEx)xdb;
			style = (Style)xdbex.metadata.legend.styles.get(object.getType());
		}
		
		return style;
	}
	
	public void enterMove(Point pt, GrView view) {
		Dynbuff.dynamicBufferClean();
		
		if (Selection.selset.size() == 0) { Dynbuff.addCross(pt, (JPanel)view); return; }
		if (view instanceof ViewGraph == false) return;
		
		ViewGraph viewGraph = (ViewGraph)view;
		GrDocGraph grDocGraph = viewGraph.getGrDocGraph();
		if (grDocGraph == null) return;
		
		XobjectEx xobj = (XobjectEx)Selection.selset.get(0);
		
		XattrGRP attrGraph = grDocGraph.getDocGraph();
		for ( XattrGIT item : attrGraph) {
			if (item instanceof XattrGXY == false) continue;
			if ( item.getParent() != xobj ) continue;
			Dynbuff.addCross(pt, (JPanel)view);
			return;
		}
		
		DynbuffText text = new DynbuffText(pt, xobj.getName(), getStyle(xobj));
		Dynbuff.dynamicBufferAdd(text, Dynbuff.Buffer.CMD, (JPanel)view);
	}
	
	private void draw(Graphics g, TcDisplayTransf transf, Color background)	{
	}
	
	public void actionPerformed(ActionEvent e) {
	}
	
}
